﻿// See https://aka.ms/new-console-template for more information
using static System.Console;

Title = "C#五种创建线程的方法";

//第一种
//new Thread(() => WriteLine("Thread线程，线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString())).Start();

//WriteLine("主线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString());



//第二种:使用线程池
//ThreadPool.QueueUserWorkItem((p) => WriteLine("线程池创建线程，线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString()));
//WriteLine("主线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString());





//第三种

//Task.Run(() => WriteLine("Task任务创建线程，线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString()));

//WriteLine("主线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString());

//TaskFactory taskFactory = new TaskFactory();

//taskFactory.StartNew(() => WriteLine("Task工厂创建线程，线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString()));



//第四种 使用委托来创建线程
//net core不能使用下面创建线程的方法

//Action action = () => { WriteLine("Action委托创建线程，线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString()); };
//action.BeginInvoke(null, null);


//WriteLine("主线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString());




//第五种 
Parallel.For(0, 10, p =>
{
    Thread.Sleep(1000);
    WriteLine("Parallel并行创建线程，线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString());
});

WriteLine("主线程ID：" + Thread.CurrentThread.ManagedThreadId.ToString());

ReadKey();